<template>
  <div class="app-container">
    <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
    >
      <el-form-item label="账户名称" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入名称"
            clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button
            type="primary"
            icon="Search"
            @click="handleQuery"
        >搜索</el-button>
      </el-form-item>
    </el-form>
    <el-button icon="Plus" type="primary" plain  @click="openDialog">添加账号</el-button>

    <el-table :data="moreArr" v-loading="loading">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="账号名称" align="center" prop="name">
        <template #default="scope">
          {{scope.row.name}}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime"  />
      <el-table-column align="center" label="操作">
        <template #default="scope">
          <el-button link size="small" type="primary" icon="Edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button link size="small" type="danger" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.size"
        @pagination="getMoreApi"
    />

    <el-dialog title="多账号管理" v-model="open" width="40%">
      <el-form class="demo-form-inline" label-width="80px" :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="账号名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入账号名称" />
        </el-form-item>
        <el-form-item label="备注说明">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer" v-loading="loading">
          <el-button @click="open=false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="More">
import {addMore, delMore, getMore, upMore} from "@/api/admin/index.js";
const {proxy}=getCurrentInstance();
const open = ref(false);
const loading = ref(false);
const total = ref(0);
const title = ref("");
const moreArr = ref([]);
const data = reactive({
  form: {},
  queryParams: {
    page: 1,
    size: 10,
  },
  rules: {
    name: [
      { required: true, message: '请输入账号名称', trigger: 'blur' },
      { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
    ]
  }
});
const { queryParams, form, rules } = toRefs(data);
async function getMoreApi(){
  try {
    loading.value=true;
    const {data,total:totalAll}=await getMore(queryParams.value);
    moreArr.value=data;
    total.value=totalAll;
  }finally {
    loading.value=false;
  }
}
async function submitForm(){
  await proxy.$refs["ruleForm"].validate();
  try {
    loading.value=true;
    !form.value.id&&await addMore(form.value);
    form.value.id&&await upMore(form.value);
    proxy.$modal.msgSuccess(form.value.id? "修改成功" : "新增成功");
    open.value = false;
    getMoreApi();
  }finally {
    loading.value=false;
  }
}
function handleQuery(){
  queryParams.page=1;
  getMoreApi();
}
function openDialog(){
  form.value={};
  open.value=true;
}

function handleEdit(row){
  form.value={...row};
  open.value=true;
}
/** 删除按钮操作 */
async function handleDelete(row) {
  await proxy.$modal.confirm(`是否删除多账号：${row.name}？`)
  await delMore({id:row.id})
  getMoreApi();
  proxy.$modal.msgSuccess("删除成功");
}

getMoreApi()
</script>

<style scoped lang="scss">

</style>